package com.platform.qujia.controller;


import com.alibaba.fastjson.JSONObject;
import com.platform.qujia.enums.OilEngineStatusEnum;
import com.platform.qujia.pojo.OilEngine;
import com.platform.qujia.pojo.dto.OilEngineDto;
import com.platform.qujia.service.OilEngineService;
import com.platform.qujia.utils.DateUtils;
import com.platform.qujia.utils.ResultData;
import com.platform.qujia.utils.StringUtils;
import com.platform.qujia.utils.exception.CustomException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/engine")
@CrossOrigin
@Api("油机数据")
@Slf4j
public class OilEngineController {

    @Autowired
    private OilEngineService oilEngineService;

    /**
     * 查询油机数据录入信息
     * 根据油枪和订单创建时间查询10分钟的订单
     * */
    @PostMapping("/list")
    @ApiOperation(value = "查询油机数据录入信息", response = OilEngine.class)
    public ResultData getOilEngineList(@RequestBody(required = false) OilEngine oilEngine){
        log.info("请求查询油机数据录入信息getOilEngineList: [{}]", JSONObject.toJSONString(oilEngine));
        List<OilEngine> oilEngineList = oilEngineService.selectOilEngineList(oilEngine);
        log.info("响应查询油机数据录入信息getOilEngineList.size: [{}]", oilEngineList.size());
        return ResultData.success(oilEngineList);
    }

    /**
     * 公众号支付油机订单后，修改订单状态和记录支付时间
     * */
    @PostMapping("/payment")
    @ApiOperation(value = "修改订单状态和记录支付时间")
    public ResultData paymentOilOrder(@RequestBody OilEngineDto oilEngineDto){
        if(Objects.isNull(oilEngineDto) || Objects.isNull(oilEngineDto.getId())){
            return ResultData.error("请求参数异常");
        }
        log.info("paymentOilOrder 修改油机订单状态：[{}]",JSONObject.toJSONString(oilEngineDto));
        OilEngine oilEngine = new OilEngine();
        BeanUtils.copyProperties(oilEngineDto, oilEngine);
        oilEngine.setStatus(OilEngineStatusEnum.PAYMENT.getValue());
        oilEngine.setPayTime(new Date());
        oilEngine.setUpdateTime(new Date());
        int i = oilEngineService.updateOilEngine(oilEngine);
        if(i <= 0){
            return ResultData.error("支付状态修改失败");
        }
        return ResultData.success("支付成功");
    }

//    /**
//     * 油机数据录入
//     * */
//    @PostMapping("/save")
//    public ResultData saveOilEngine(@RequestBody(required = false) OilEngineDto oilEngineDto){
//        if(Objects.isNull(oilEngineDto)){
//            return ResultData.error("油机数据录入请求参数异常",null);
//        }
//        log.info("请求油机数据录入saveOilEngine：[{}]",JSONObject.toJSONString(oilEngineDto));
//        OilEngine oilEngine = checkOilEngine(oilEngineDto);
//        log.info("保存油机数据录入saveOilEngine：[{}]",JSONObject.toJSONString(oilEngine));
//        int i = oilEngineService.saveOilEngine(oilEngine);
//        if(i <= 0){
//            return ResultData.error("油机数据录入失败",null);
//        }
//        return ResultData.success();
//    }
//
//    /**
//     * 查询油机数据录入信息
//     * */
//    @PostMapping("/list")
//    public ResultData getOilEngineList(@RequestBody(required = false) OilEngine oilEngine){
//        log.info("请求查询油机数据录入信息getOilEngineList: [{}]", JSONObject.toJSONString(oilEngine));
//        List<OilEngine> oilEngineList = oilEngineService.selectOilEngineList(oilEngine);
//        log.info("响应查询油机数据录入信息getOilEngineList: [{}]", JSONObject.toJSONString(oilEngineList));
//        return ResultData.success(oilEngineList);
//    }
//
//
//    // 参数校验
//    private OilEngine checkOilEngine(OilEngineDto oilEngineDto){
//        OilEngine oilEngine = new OilEngine();
//        if(StringUtils.isEmpty(oilEngineDto.getPassageway())){
//            throw new CustomException("上报数据失败，通道参数为空");
//        }
//        oilEngine.setPassageway(oilEngineDto.getPassageway());
//
//        if(StringUtils.isEmpty(oilEngineDto.getCardNum())){
//            throw new CustomException("上报数据失败，卡号参数为空");
//        }
//        oilEngine.setCardNum(oilEngineDto.getCardNum());
//
//        if(StringUtils.isEmpty(oilEngineDto.getOrderNum())){
//            throw new CustomException("上报数据失败，订单号参数为空");
//        }
//        oilEngine.setOrderNum(oilEngineDto.getOrderNum());
//
//        if(StringUtils.isEmpty(oilEngineDto.getOilPersonnelId())){
//            throw new CustomException("上报数据失败，加油员id参数为空");
//        }
//        oilEngine.setOilPersonnelId(oilEngineDto.getOilPersonnelId());
//
//        if(StringUtils.isEmpty(oilEngineDto.getOilGun())){
//            throw new CustomException("上报数据失败，油枪参数为空");
//        }
//        oilEngine.setOilGun(oilEngineDto.getOilGun());
//
//        if(StringUtils.isEmpty(oilEngineDto.getOilNameNum())){
//            throw new CustomException("上报数据失败，油品编号参数为空");
//        }
//        oilEngine.setOilNameNum(oilEngineDto.getOilNameNum());
//
//        if(StringUtils.isEmpty(oilEngineDto.getOilName())){
//            throw new CustomException("上报数据失败，油品参数为空");
//        }
//        oilEngine.setOilName(oilEngineDto.getOilName());
//
//
//        if(StringUtils.isEmpty(oilEngineDto.getOilPrice())){
//            throw new CustomException("上报数据失败，单价参数为空");
//        }
//        oilEngine.setOilPrice(new BigDecimal(oilEngineDto.getOilPrice()));
//
//
//        if(StringUtils.isEmpty(oilEngineDto.getTotalPrice())){
//            throw new CustomException("上报数据失败，总价参数为空");
//        }
//        oilEngine.setTotalPrice(new BigDecimal(oilEngineDto.getTotalPrice()));
//
//
//        if(StringUtils.isEmpty(oilEngineDto.getBalanceAmount())){
//            throw new CustomException("上报数据失败，卡余额参数为空");
//        }
//        oilEngine.setBalanceAmount(new BigDecimal(oilEngineDto.getBalanceAmount()));
//
//        if(StringUtils.isEmpty(oilEngineDto.getOrderLiters())){
//            throw new CustomException("上报数据失败，数量参数为空");
//        }
//        oilEngine.setOrderLiters(Double.valueOf(oilEngineDto.getOrderLiters()));
//
//
//        if(StringUtils.isEmpty(oilEngineDto.getStartFlowMeter())){
//            throw new CustomException("上报数据失败，开始泵码参数为空");
//        }
//        oilEngine.setStartFlowMeter(Double.valueOf(oilEngineDto.getStartFlowMeter()));
//
//
//        if(StringUtils.isEmpty(oilEngineDto.getEndFlowMeter())){
//            throw new CustomException("上报数据失败，结束泵码参数为空");
//        }
//        oilEngine.setEndFlowMeter(Double.valueOf(oilEngineDto.getEndFlowMeter()));
//
//        if(StringUtils.isNotEmpty(oilEngineDto.getEnterpriseId())){
//            oilEngine.setEnterpriseId(Long.valueOf(oilEngineDto.getEnterpriseId()));
//        }
//
//        if(StringUtils.isNotEmpty(oilEngineDto.getStationId())){
//            oilEngine.setStationId(Long.valueOf(oilEngineDto.getStationId()));
//        }
//
//        if(StringUtils.isNotEmpty(oilEngineDto.getStationName())){
//            oilEngine.setStationName(oilEngineDto.getStationName());
//        }
//
//        if(StringUtils.isNotEmpty(oilEngineDto.getOrderTime())) {
//
//            oilEngine.setOrderTime(DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, oilEngineDto.getOrderTime()));
//        }
//        return oilEngine;
//    }

}
